import 'dart:async';
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/config/app_config.dart';
import 'package:flutter_douban/config/event_bus_util.dart';
import 'package:flutter_douban/main_page/me/page/setting_page.dart';
import 'package:flutter_douban/main_page/me/view/has_login_view.dart';
import 'package:flutter_douban/main_page/me/view/user_drawer_view.dart';
import 'package:flutter_douban/tool_views/app_dialog.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'login_page.dart';

class MeCenterPage extends StatefulWidget {
  const MeCenterPage({Key? key}) : super(key: key);

  @override
  _MeCenterPageState createState() => _MeCenterPageState();
}

class _MeCenterPageState extends State<MeCenterPage> {
  bool isShow = false;
  String? _numberId;
  StreamSubscription<LoginEvent>? _loginSubscription;
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  /// 判断是否登录
  Future<String?> getUserNumberId() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    _numberId = prefs.getString(AppConfig.numberId);
    if (_numberId == null) {
      _numberId = '';
    }
    return _numberId;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _loginSubscription = EventBusUtil.listen((event) {
      setState(() {
        getUserNumberId();
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<String?>(
        future: getUserNumberId(),
        builder: (context, snapshot) {
          if (!snapshot.hasData)
            return Center(child: CircularProgressIndicator()); // 数据不存在，加载选型进度条
          if (snapshot.error != null)
            return Center(child: Text("请求失败")); // 请求失败
          String? string = snapshot.data;

          /// 登录方式视图
          Widget rowWidget;
          if (isShow == true) {
            rowWidget = Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                FlatButton(
                    onPressed: () {},
                    child: Image.asset(
                      'assets/images/common/login_wechat@2x.png',
                      width: 30,
                      height: 30,
                    )),
                FlatButton(
                    onPressed: () {},
                    child: Image.asset(
                      'assets/images/common/login_qq@2x.png',
                      width: 30,
                      height: 30,
                    )),
                FlatButton(
                    onPressed: () {},
                    child: Image.asset(
                      'assets/images/common/login_sina@2x.png',
                      width: 30,
                      height: 30,
                    )),
                FlatButton(
                    onPressed: () {},
                    child: Image.asset(
                      'assets/images/common/login_phone@2x.png',
                      width: 30,
                      height: 30,
                    )),
              ],
            );
          } else {
            rowWidget = Container(
              width: MediaQuery.of(context).size.width,
              height: 1,
              color: Colors.white,
            );
          }

          /// 个人中心界面主视图
          Widget meMianWidget;
          if (_numberId == '') {
            meMianWidget = Container(
              color: Colors.white,
              alignment: Alignment.center,
              child: ListView(
                children: [
                  Container(
                    padding: EdgeInsets.only(top: 20, left: 30),
                    width: 100,
                    child: Text(
                      '豆果美食\n会做饭很酷~',
                      style: TextStyle(
                          fontSize: 32,
                          fontWeight: FontWeight.bold,
                          color: Colors.black),
                    ),
                  ),
                  SizedBox(
                    height: MediaQuery.of(context).size.width / 2,
                  ),
                  InkWell(
                    child: Container(
                      alignment: Alignment.center,
                      margin: EdgeInsets.only(left: 20, right: 20),
                      height: 44,
                      decoration: BoxDecoration(
                        color: AppColors.yellow,
                        borderRadius: BorderRadius.circular(22),
                      ),
                      child: Text(
                        '手机号登录',
                        style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.bold,
                            color: Colors.black),
                      ),
                    ),
                    onTap: () {
                      Navigator.of(context)
                          .push(MaterialPageRoute(builder: (_) {
                        return LoginPage();
                      }));
                    },
                  ),
                  SizedBox(
                    height: 15,
                  ),
                  InkWell(
                    child: Container(
                      alignment: Alignment.center,
                      margin: EdgeInsets.only(left: 20, right: 20),
                      height: 44,
                      decoration: BoxDecoration(
                        color: AppColors.white,
                        borderRadius: BorderRadius.circular(22),
                        border:
                            new Border.all(color: AppColors.black, width: 2),
                      ),
                      child: Text(
                        '通过 Apple 登录',
                        style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.bold,
                            color: Colors.black),
                      ),
                    ),
                    onTap: () {},
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  FlatButton(
                      onPressed: () {
                        setState(() {
                          isShow = !isShow;
                        });
                      },
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            '其他登录方式 ',
                            style:
                                TextStyle(fontSize: 13, color: Colors.black45),
                          ),
                          Image.asset(
                            isShow == true
                                ? 'assets/images/common/icon_topic_up@2x.png'
                                : 'assets/images/common/icon_topic_down@2x.png',
                            width: 14,
                            height: 14,
                            color: Colors.black45,
                          )
                        ],
                      )),
                  rowWidget
                ],
              ),
            );
          } else {
            meMianWidget = HasLoginView(numberId: _numberId!,);
          }

          return Scaffold(
              key: _scaffoldKey,
              appBar: AppBar(
                backgroundColor: _numberId!.length > 0 ?AppColors.yellow : Colors.white,
                leadingWidth: 45,
                leading: IconButton(
                    onPressed: () {
                      if(_numberId!.length > 0){
                        // AppDialog.showText(context, title: "功能暂未开放");
                        _scaffoldKey.currentState!.openDrawer();
                      }else{
                        Navigator.of(context)
                            .push(MaterialPageRoute(builder: (_) {
                          return SettingPage();
                        }));
                      }
                    },
                    alignment: Alignment.centerRight,
                    icon: Image.asset(
                        _numberId != ''
                            ? 'assets/images/common/icon_home_category.png':'assets/images/common/navi_item_setting@2x.png',
                        width: 22,
                        height: 22,
                        color: Colors.black)),
                actions: [
                  IconButton(
                      onPressed: () {},
                      alignment: Alignment.centerLeft,
                      icon: Image.asset(
                        'assets/images/common/navi_item_messages.png',
                        width: 22,
                        height: 22,
                        color: Colors.black,
                      )),
                ],
              ),
              drawer: UserDrawerView(),
              body: meMianWidget);
        });
  }
}
